package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.service.IDividendRecordService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.DividendRecordBo;
import org.locker.domain.vo.DividendRecordVo;
import org.locker.domain.DividendRecord;
import org.locker.mapper.DividendRecordMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 管理员分润记录Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
public class DividendRecordServiceImpl  extends ServiceImpl<DividendRecordMapper,DividendRecord>  implements IDividendRecordService {

    private final DividendRecordMapper baseMapper;

    /**
     * 查询管理员分润记录
     */
    @Override
    public DividendRecordVo queryById(Long dividendId){
        return baseMapper.selectVoById(dividendId);
    }

    /**
     * 查询管理员分润记录列表
     */
    @Override
    public TableDataInfo<DividendRecordVo> queryPageList(DividendRecordBo bo, PageQuery pageQuery) {
        Page<DividendRecordVo> result = baseMapper.listByPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询管理员分润记录列表
     */
    @Override
    public List<DividendRecordVo> queryList(DividendRecordBo bo) {
        LambdaQueryWrapper<DividendRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DividendRecord> buildQueryWrapper(DividendRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DividendRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPositionId() != null, DividendRecord::getPositionId, bo.getPositionId());
        lqw.eq(bo.getManagerId() != null, DividendRecord::getManagerId, bo.getManagerId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), DividendRecord::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getAmount() != null, DividendRecord::getAmount, bo.getAmount());
        lqw.eq(bo.getDividendParentAmount() != null, DividendRecord::getDividendParentAmount, bo.getDividendParentAmount());
        return lqw;
    }

    /**
     * 新增管理员分润记录
     */
    @Override
    public Boolean insertByBo(DividendRecordBo bo) {
        DividendRecord add = MapstructUtils.convert(bo, DividendRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDividendId(add.getDividendId());
        }
        return flag;
    }

    /**
     * 修改管理员分润记录
     */
    @Override
    public Boolean updateByBo(DividendRecordBo bo) {
        DividendRecord update = MapstructUtils.convert(bo, DividendRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DividendRecord entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除管理员分润记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
